using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace BizTalk.BuildGenerator.MetaData
{
    /// <summary>
    /// Represents the BizTalk hosts required for this Build
    /// </summary>
    [Serializable]
    public class BizTalkHosts
    {
        private List<BizTalkHost> _hosts = new List<BizTalkHost>();

        /// <summary>
        /// The hosts
        /// </summary>
        public List<BizTalkHost> Hosts
        {
            get { return _hosts; }
            set { _hosts = value; }
        }
    }
    /// <summary>
    /// Represents a BizTalk host
    /// </summary>
    [Serializable]
    public class BizTalkHost
    {
        private string _name;
        private BizTalkHostType _hostType = BizTalkHostType.InProcess;
        private string _username;
        private string _password;
        private List<BizTalkAdapterHandler> _sendHandlers = new List<BizTalkAdapterHandler>();
        private List<BizTalkAdapterHandler> _receiveHandlers = new List<BizTalkAdapterHandler>();
        private string _windowsGroupName = "BizTalk Application Users";
        private bool _trackingEnabled;
        private bool _hostTrusted = true;

        /// <summary>
        /// Overrides to string
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.IsNullOrEmpty(_name) ? base.ToString() : _name;
        }

        /// <summary>
        /// The windows group associated with the host
        /// </summary>                
        [Description("The windows group associated with this host")]
        public string WindowsGroupName
        {
            get { return _windowsGroupName; }
            set { _windowsGroupName = value; }
        }
        /// <summary>
        /// The receive handlers associated with the host
        /// </summary>
        [Description("The list of receive handlers for this host")]
        public List<BizTalkAdapterHandler> ReceiveHandlers
        {
            get { return _receiveHandlers; }
            set { _receiveHandlers = value; }
        }
        /// <summary>
        /// The send handlers associated with the host
        /// </summary>
        [Description("The list of send handlers for this host")]
        public List<BizTalkAdapterHandler> SendHandlers
        {
            get { return _sendHandlers; }
            set { _sendHandlers = value; }
        }
        /// <summary>
        /// The password for the hosts credentials
        /// </summary>
        [Description("The password for the instance of this host")]
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }
        /// <summary>
        /// The user name of the hosts credentials
        /// </summary>
        [Description("The username for the instance of this host")]
        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }
        /// <summary>
        /// The type of the host
        /// </summary>
        [Description("The type of this host")]
        public BizTalkHostType HostType
        {
            get { return _hostType; }
            set { _hostType = value; }
        }
        /// <summary>
        /// The name of the host
        /// </summary>
        [Description("The name of the host")]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }        
        /// <summary>
        /// Trusted
        /// </summary>
        [Description("Indicates if the host is trusted")]
        public bool HostTrusted
        {
            get { return _hostTrusted; }
            set { _hostTrusted = value; }
        }
        /// <summary>
        /// Tracking enabled
        /// </summary>
        [Description("Indicates if the host tracking is enabled")]
        public bool TrackingEnabled
        {
            get { return _trackingEnabled; }
            set { _trackingEnabled = value; }
        }

        /// <summary>
        /// Checks if a handler already exists for this adapter
        /// </summary>
        /// <param name="adapterName"></param>
        /// <param name="handlers"></param>
        /// <returns></returns>
        public static bool ContainsHandler(string adapterName, List<BizTalkAdapterHandler> handlers)
        {
            return handlers.Any(handler => adapterName == handler.AdapterName);
        }

        /// <summary>
        /// Adds any missing adapters for example if any were added since last run
        /// </summary>
        public void RefreshAdapters()
        {
            var adapters = BizTalkHelper.GetAdapters();

            //Add missing adapters                
            foreach (var adapter in adapters)
            {
                if (!ContainsHandler(adapter, SendHandlers))
                    SendHandlers.Add(new BizTalkAdapterHandler(adapter));

                if (!ContainsHandler(adapter, ReceiveHandlers))
                    ReceiveHandlers.Add(new BizTalkAdapterHandler(adapter));
            }
        }
    }
    /// <summary>
    /// The types of BizTalk Host
    /// </summary>
    public enum BizTalkHostType
    {
        /// <summary>
        /// Represents an in process host
        /// </summary>
        InProcess,
        /// <summary>
        /// Represets an isolated host
        /// </summary>
        Isolated,
    }

    public class BizTalkAdapterHandler
    {
        private string _adapterName;

        public BizTalkAdapterHandler()
        {

        }

        public BizTalkAdapterHandler(string adapter)
        {
            _adapterName = adapter;
        }

        public bool Included { get; set; }

        /// <summary>
        /// The name of the adapter
        /// </summary>
        public string AdapterName
        {
            get { return _adapterName; }
            set { _adapterName = value; }
        }

        public override string ToString()
        {
            return string.IsNullOrEmpty(_adapterName) ? base.ToString() : _adapterName;
        }
    }
}
